package com.school.untils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @program:
 * @description: 常量
 * @author: CaoAMan
 * @create: 2020-12-09 17:03
 **/
public class AuthMDA {
    public static final int COMM_ROLE_ID = 1;

    public static final int COMM_AUTHORITY_ID = 1;
    public static final String UPLOAD_PATH = "/uploads/";

    public static String genRandomNum() {
        int maxNum = 36;
        int i;
        int count = 0;
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < 6) {
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }
        return pwd.toString();
    }
    // 设置文件路径
    public static List<String> setFiles(List<MultipartFile> fileList,String basePath) throws IOException {
        List<String> filesList = new ArrayList<>();
        for(int i = 0; i < fileList.size(); i ++){
            String fileName = AuthMDA.createFileName(i,fileList.get(i));
            String filePath = basePath + fileName;
            boolean upload = AuthMDA.upload(filePath, fileList.get(i));
            if (upload) {
                String files = UPLOAD_PATH + fileName;
                filesList.add(files);
            } else{
                filesList.add("");
            }
        }
        return filesList;
    }
    // 文件上传到指定位置
    public static boolean upload(String filePath, MultipartFile file) throws IOException {
        FileOutputStream fileOutputStream = null;
        if (file != null) {
            try {
                File file1 = new File(filePath);
                if (!file1.exists()) {
                    file1.createNewFile();
                }
                fileOutputStream = new FileOutputStream(file1);
                fileOutputStream.write(file.getBytes());
                fileOutputStream.flush();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            } finally {
                fileOutputStream.close();
            }
        } else {
            return false;
        }
    }
    // 创建文件名
    public static String createFileName(int count,MultipartFile file) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileType  = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
        String fileName = simpleDateFormat.format(date) + "-" + count + fileType;
        return fileName;
    }

    // 去除[]括号
    public static String removeBrackets(String filesList){
        return filesList.replace("[","").replace("]","");
    }
}
